#include "Arrow.h"

Arrow::Arrow(shared_ptr<Man > DigDug):

    _DigDug(DigDug)
{
    _arrowExpand = 0;
    _expandable = true;
    _invisible = false;

    _expand = false;
    _contract = false;
    _shootDirection = 0;
}

void Arrow::initialise()
{
    GameElement::initialise(650,350,0,0,ARROW);
}

void Arrow::set_expand()
{
    if (_expandable)
    {
        _expand = true;
        _expandable = false;
    }
}

void Arrow::update()
{
    if (_expand)
    {
        if (_arrowExpand < 15)
        {
            _arrowExpand ++;
        }

        else if (_arrowExpand == 15)
        {
            _expand = false;
            _contract = true;
        }
    }

    else if (_contract)
    {
        if (_arrowExpand > 0)
        {
            _arrowExpand --;
        }

        else if (_arrowExpand == 0)
        {
            _expandable = true;
        }
    }

    if (_arrowExpand == 0)
    {
        _invisible = true;
    }
    else _invisible = false;

    update_head_pos();
}

void Arrow::update_head_pos()
{
    if ( _DigDug->get_direction_x() > 0 ) //right
    {
        _shootDirection = 1;

        _pos_x = _DigDug->get_pos_x() + (10*_arrowExpand) + 50;
        _pos_y = _DigDug->get_pos_y() + 36;

    }

    else if ( _DigDug->get_direction_x() < 0 ) //left
    {
        _shootDirection = 2;

        _pos_x = _DigDug->get_pos_x() - (10*_arrowExpand);
        _pos_y = _DigDug->get_pos_y() + 36;

    }

    else if ( _DigDug->get_direction_y() < 0 ) //up
    {
        _shootDirection = 3;

        _pos_x = _DigDug->get_pos_x() +36;
        _pos_y = _DigDug->get_pos_y() - (10*_arrowExpand);
    }

    else if ( _DigDug->get_direction_y() > 0 ) //down
    {
        _shootDirection = 4;

        _pos_x = _DigDug->get_pos_x() + 14;
        _pos_y = _DigDug->get_pos_y() + (10*_arrowExpand) + 50;
    }


    if ( _DigDug->get_direction_x() == 0 && _DigDug->get_direction_y() == 0)
    {
        if(_shootDirection == 1)
        {
            _pos_x = _DigDug->get_pos_x() + (10*_arrowExpand) + 50;
            _pos_y = _DigDug->get_pos_y() + 36;
        }

        else if (_shootDirection == 2)
        {
            _pos_x = _DigDug->get_pos_x() - (10*_arrowExpand);
            _pos_y = _DigDug->get_pos_y() + 36;
        }

        else if (_shootDirection == 3)
        {
            _pos_x = _DigDug->get_pos_x() +36;
            _pos_y = _DigDug->get_pos_y() - (10*_arrowExpand);
        }

        else if (_shootDirection == 4)
        {
            _pos_x = _DigDug->get_pos_x() + 14;
            _pos_y = _DigDug->get_pos_y() + (10*_arrowExpand) + 50;
        }
    }

    if (_invisible)
    {
        _pos_x = 0;
        _pos_y = 0;
    }
}

bool Arrow::check_collision(int monster_pos_x, int monster_pos_y)
{
    if ( (_pos_x < monster_pos_x+50)       &&
         (_pos_x > monster_pos_x)          &&
         (_pos_y < monster_pos_y+50)       &&
         (_pos_y > monster_pos_y)          )
    {
        return true;
    }

    else return false;
}

bool Arrow::get_invisible()
{
    return _invisible;
}

double Arrow::get_arrowExpand()
{
    return _arrowExpand;
}

